﻿#include "OiPCH.hpp"

namespace Oi
{
    namespace PLScannerCore
    {
        class PresetDlgImpl : public Impl<PresetDlg>
        {
        public:
            void init();

            void update();

            void addItem(QString name);

            void setCurrent(QString name);

        public:
            QLineEdit* _searchName;
            QPushButton* _clearSearch;

            QListWidget* _formulaList;

            QPushButton* _appendButton;
            QPushButton* _modifyButton;
            QPushButton* _deleteButton;

            QPushButton* _completeButton;
            QPushButton* _cancelButton;

            PresetWidget* _productViewer;

        public:
            QMap<QString, QListWidgetItem*> _items;
            QString _currentProduct;
            int _operator;
        };

        void PresetDlgImpl::init()
        {
            OI_Q(PresetDlg);
            q->setWindowTitle(PresetDlg::tr("预设管理器"));

            auto font = q->font();
            font.setPixelSize(18);

            QHBoxLayout* formulaLayout = new QHBoxLayout(q);
            formulaLayout->setContentsMargins(2, 0, 2, 0);
            formulaLayout->setSpacing(2);
            
            auto listWidget = new QWidget();
            listWidget->setMinimumWidth(200);
            QVBoxLayout* formulaListLayout = new QVBoxLayout(listWidget);
            formulaListLayout->setContentsMargins(0, 0, 0, 0);
            formulaListLayout->setSpacing(2);
 
            QHBoxLayout* searchNameLayout = new QHBoxLayout();
            searchNameLayout->setContentsMargins(0, 0, 0, 0);
            searchNameLayout->setSpacing(2);
            _searchName = new QLineEdit();
            _searchName->setObjectName("_searchName");
            _searchName->setPlaceholderText(PresetDlg::tr("预设搜索关键字"));
            _searchName->setFont(font);

            _clearSearch = new QPushButton(PresetDlg::tr("清空"));
            _clearSearch->setObjectName("_clearSearch");
            _clearSearch->setFont(font);
            searchNameLayout->addWidget(_searchName, 1);
            searchNameLayout->addWidget(_clearSearch);

            _formulaList = new QListWidget();
            _formulaList->setObjectName("_formulaList");
            _formulaList->setGridSize(QSize(260, 55));
            _formulaList->setResizeMode(QListView::Adjust);
            _formulaList->setDragDropMode(QListWidget::NoDragDrop);
            _formulaList->setSelectionMode(QListWidget::SingleSelection);
            _formulaList->setMovement(QListView::Static);
            _formulaList->setViewMode(QListWidget::ListMode);
            _formulaList->setSpacing(5);
            _formulaList->setContextMenuPolicy(Qt::CustomContextMenu);
            _formulaList->setFrameShape(QFrame::NoFrame);

            auto buttonsWidget = new QWidget();
            QHBoxLayout* formulaButtonLayout = new QHBoxLayout(buttonsWidget);
            formulaButtonLayout->setContentsMargins(0, 0, 0, 0);
            formulaButtonLayout->setSpacing(2);
                    
            _appendButton = new QPushButton(q);
            _appendButton->setObjectName("_appendButton");
            _appendButton->setText(PresetDlg::tr("添加"));
            _appendButton->setFont(font);
            _modifyButton = new QPushButton(q);
            _modifyButton->setObjectName("_modifyButton");
            _modifyButton->setText(PresetDlg::tr("修改"));
            _modifyButton->setFont(font);
            _deleteButton = new QPushButton(q);
            _deleteButton->setText(PresetDlg::tr("删除"));
            _deleteButton->setObjectName("_deleteButton");
            _deleteButton->setFont(font);
            _completeButton = new QPushButton(q);
            _completeButton->setText(PresetDlg::tr("保存"));
            _completeButton->setObjectName("_completeButton");
            _completeButton->setFont(font);
            _cancelButton = new QPushButton(q);
            _cancelButton->setText(PresetDlg::tr("取消"));
            _cancelButton->setObjectName("_cancelButton");
            _cancelButton->setFont(font);
            formulaButtonLayout->addWidget(_appendButton);
            formulaButtonLayout->addWidget(_modifyButton);
            formulaButtonLayout->addWidget(_deleteButton);
            formulaButtonLayout->addWidget(_completeButton);
            formulaButtonLayout->addWidget(_cancelButton);

            formulaListLayout->addLayout(searchNameLayout);
            formulaListLayout->addWidget(_formulaList, 1);
            formulaListLayout->addWidget(buttonsWidget);


            _productViewer = new PresetWidget(q);

            formulaLayout->addWidget(_productViewer, 1);
            formulaLayout->addWidget(listWidget);


            _completeButton->hide();
            _cancelButton->hide();

            _operator = 0;

            QMetaObject::connectSlotsByName(q);
        }

        void PresetDlgImpl::update()
        {
            _items.clear();

            _formulaList->blockSignals(true);
            _formulaList->clear();
            _formulaList->blockSignals(false);

            auto names = PresetParamsManager::instance()->presetNames();
            for (auto name : names)
            {
                addItem(name);
            }
            
            setCurrent(_currentProduct);
        }

        void PresetDlgImpl::addItem(QString name)
        {
            _formulaList->blockSignals(true);

            QListWidgetItem* item = new QListWidgetItem(_formulaList);
            item->setFlags(Qt::ItemIsTristate | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
            item->setData(Qt::UserRole + 1, name);
            item->setSizeHint(QSize(250, 50));
            ListItemLabel* label = new ListItemLabel();
            label->setDispText(name);
            _formulaList->setItemWidget(item, label);

            _formulaList->blockSignals(false);

            _items[name] = item;
        }

        void PresetDlgImpl::setCurrent(QString name)
        {
            _formulaList->blockSignals(true);

            _currentProduct = name;

            if (name.isEmpty() ||
                !_items.contains(name))
            {
                _formulaList->setCurrentRow(-1);
                PresetParams params;
                _productViewer->setParams(name, params);
            }
            else
            {
                _formulaList->setCurrentItem(_items[name]);
                PresetParams params = PresetParamsManager::instance()->params(name);
                _productViewer->setParams(name, params);
            }

            _formulaList->blockSignals(false);

        }

        PresetDlg::PresetDlg(QWidget* parent/* = NULL*/)
            : Ctrl::Dialog(parent)
        {
            OI_I(PresetDlg)->init();
            PresetParamsManager::instance();
            updatePage();
        }

        PresetDlg::~PresetDlg()
        {
            PresetParamsManager::instance()->saveParams();
        }

        void PresetDlg::updatePage()
        {
            OI_IMPL()->update();
        }

        void PresetDlg::setCurrent(QString name)
        {
            OI_IMPL()->setCurrent(name);
        }

        void PresetDlg::on__searchName_textChanged(const QString &name)
        {
            OI_F(PresetDlg);

            auto rows = d->_formulaList->count();

            for(auto i = 0; i < rows; i++)
            {
                auto item = d->_formulaList->item(i);
                auto label = reinterpret_cast<ListItemLabel*>( d->_formulaList->itemWidget(item) );
                if(label->containsText(name))
                {
                    item->setHidden(false);
                    label->setSelectText(name);
                }
                else
                {
                    item->setHidden(true);
                }
            }
        }

        void PresetDlg::on__clearSearch_clicked()
        {
            OI_F(PresetDlg);

            d->_searchName->setText(QString(""));
        }

        void PresetDlg::on__formulaList_currentRowChanged(int row)
        {
            OI_F(PresetDlg);

            if (row >= 0)
            {
                d->setCurrent(d->_formulaList->item(row)->data(Qt::UserRole + 1).toString());
            }
            else
            {
                d->setCurrent("");
            }
        }

        void PresetDlg::on__appendButton_clicked()
        {
            OI_F(PresetDlg);
            d->_operator = 1;
            d->_formulaList->setEnabled(false);
            d->_appendButton->hide();
            d->_modifyButton->hide();
            d->_deleteButton->hide();
            d->_completeButton->show();
            d->_cancelButton->show();

            PresetParams params;

            QString defaultName = PresetDlg::tr("未命名");
            if(PresetParamsManager::instance()->presetNames().contains(defaultName))
            {
                for(int i = 0; i < 99; i++)
                {
                    auto temp = defaultName + "_" + QString::number(i);
                    if(!PresetParamsManager::instance()->presetNames().contains(temp))
                    {
                        defaultName = temp;
                        break;
                    }
                }
            }

            d->_productViewer->setParams(defaultName, params);
            d->_productViewer->setEditable(true);
        }

        void PresetDlg::on__modifyButton_clicked()
        {
            OI_F(PresetDlg);

            d->_operator = 2;

            d->_formulaList->setEnabled(false);
            d->_appendButton->hide();
            d->_modifyButton->hide();
            d->_deleteButton->hide();
            d->_completeButton->show();
            d->_cancelButton->show();

            d->_productViewer->setEditable(true);
        }

        void PresetDlg::on__deleteButton_clicked()
        {
            OI_F(PresetDlg);

            if (!d->_currentProduct.isEmpty())
            {
                PresetParamsManager::instance()->delParams(d->_currentProduct);
            }
            
            d->_currentProduct.clear();

            d->update();
        }

        void PresetDlg::on__completeButton_clicked()
        {
            OI_F(PresetDlg);

            if (d->_operator == 1)
            {
                if (!PresetParamsManager::instance()->addParams(d->_productViewer->name(),  d->_productViewer->parmas()))
                {
                    OiAlerter() << tr("新建产品失败！");
                }
                else
                {
                    d->_currentProduct = d->_productViewer->name();
                }
            }
            else if (d->_operator == 2)
            {
                if (d->_currentProduct == d->_productViewer->name())
                {
                    PresetParamsManager::instance()->delParams(d->_currentProduct);
                }

                if(!PresetParamsManager::instance()->addParams(d->_productViewer->name(),  d->_productViewer->parmas()))
                {
                    OiAlerter() << tr("修改产品失败！");
                }
                else if(d->_currentProduct != d->_productViewer->name())
                {
                    PresetParamsManager::instance()->delParams(d->_currentProduct);
                    d->_currentProduct = d->_productViewer->name();
                }
            }

            d->_formulaList->setEnabled(true);
            d->_appendButton->show();
            d->_modifyButton->show();
            d->_deleteButton->show();
            d->_completeButton->hide();
            d->_cancelButton->hide();

            d->_productViewer->setEditable(false);

            d->update();
        }

        void PresetDlg::on__cancelButton_clicked()
        {
            OI_F(PresetDlg);

            d->_formulaList->setEnabled(true);
            d->_appendButton->show();
            d->_modifyButton->show();
            d->_deleteButton->show();
            d->_completeButton->hide();
            d->_cancelButton->hide();

            d->_productViewer->setEditable(false);

            d->update();
        }

    }
}